package com.shengyin.ec.service.news;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shengyin.ec.model.dao.news.NewsDao;
import com.shengyin.ec.model.entity.News;

@Service(value ="newsService")
public class NewsServiceImpl implements NewsService{

	@Autowired
	NewsDao newsDao;
	
	
	public List<News> getNewsList(){
		List<News> newsList = newsDao.loadAll();
		return newsList;
	}


	@Override
	public void createNews(News news) {
	      newsDao.save(news);
	}


	@Override
	public void removeNewsById(long id) {
		newsDao.deleteByKey(id);
	}


	@Override
	public void modifyNews(News news) {
		
		News news1 = newsDao.get(news.getId());
		try {
			BeanUtils.copyProperties(news1, news);
			newsDao.update(news1);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public News getNews(long id){
		News news = newsDao.get(id);
		
		return news;
	}


	@Override
	public void deleteAllNews(String[] ids) {
		
	}


	@Override
	public List<News> getNewsListByDate() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public void deleteSelectedNews(String[] ids) {
		if(ids!=null){
		Long[] idsl=new Long[ids.length];
		for(int i=0;i<ids.length;i++){
			if(ids[i]!=null||!ids[i].isEmpty()){
				idsl[i]=Long.valueOf(ids[i].trim());
			}
			
		}
		newsDao.deleteSelectedNews(idsl);
		}
	}


	@Override
	public List<News> getNewsList(int offset, int pagesize) {
		return newsDao.getNewsList(offset, pagesize);
	}


	@Override
	public Long getTotal() {
		return newsDao.getTotal();
	}
}
